package com.holim.test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MyThread extends Activity
				implements View.OnClickListener {
	int nProgress;
	ProgressBar bar;
	Button btnNoThread;
	Button btnThread01;
	Button btnThread02;
	Button btnThread03;
	Button btnThread04;
	
	Handler handle;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
        
        bar = (ProgressBar)findViewById(R.id.progressBar);
        btnNoThread = (Button)findViewById(R.id.btnNoThread);
        btnThread01 = (Button)findViewById(R.id.btnThread01);
        btnThread02 = (Button)findViewById(R.id.btnThread02);
        btnThread03 = (Button)findViewById(R.id.btnThread03);
        btnThread04 = (Button)findViewById(R.id.btnThread04);
        
        btnNoThread.setOnClickListener(this);
        btnThread01.setOnClickListener(this);
        btnThread02.setOnClickListener(this);
        btnThread03.setOnClickListener(this);
        btnThread04.setOnClickListener(this);
        
        handle = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				Log.e("", "handleMessage>>"+msg);
				super.handleMessage(msg);
				bar.incrementProgressBy(20);
			}
        	
        };
    }

	public void onClick(View v) {
		nProgress = 0;
		
		if(v == btnNoThread) {
			for(nProgress = 0; nProgress<=100; nProgress+=10) {
				bar.setProgress(nProgress);
				SystemClock.sleep(500);
			}		
		}
		// �߸�� thread. Worker thread���� UI ��ü�� ���� update��.
		else if (v == btnThread01) {
			new Thread(new Runnable() {
				public void run() {
					
					for(nProgress = 0; nProgress<=100; nProgress+=10) {
						try {
							bar.setProgress(nProgress);
						}
						catch(Exception e) {
							Log.w("h-exception", e.toString());							
						}
						SystemClock.sleep(500);
					}
				}
			}).start();
		}
		// using View.post() method
		else if (v == btnThread02) {
			new Thread(new Runnable() {
				public void run() {
					
					for(nProgress = 0; nProgress<=100; nProgress+=10) {
						bar.post(new Runnable() {
							public void run() {
								bar.setProgress(nProgress);
							}
						});	
						SystemClock.sleep(500);
					}
				}
			}).start();
		}
		// using Activity:runOnUiThread() method
		else if (v == btnThread03) {
			new Thread(new Runnable() {
				public void run() {
					
					for(nProgress = 0; nProgress<=100; nProgress+=10) {
						runOnUiThread(new Runnable() {
							public void run() {
								bar.setProgress(nProgress);
							}
						});	
						SystemClock.sleep(500);
					}
				}
			}).start();
		}
		else if (v == btnThread04) {
			bar.setProgress(0);
			Thread worker = new Thread(new Runnable() {
				public void run() {
				
					for(nProgress = 0; nProgress<=100; nProgress+=10) {
						handle.sendMessage(handle.obtainMessage());
						SystemClock.sleep(500);
					}
				}
			});
			
			worker.start();
		}		
	}
	

    
    
}